package com.detail.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.common.pojo.detail.Record;
import com.common.pojo.detail.RecordGroup;
import com.detail.R;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class RecordGroupAdapter extends RecyclerView.Adapter<RecordGroupAdapter.GroupViewHolder> {
    private List<RecordGroup> groups;
    private SimpleDateFormat daySdf = new SimpleDateFormat("MM月dd日 E", Locale.CHINA);

    public RecordGroupAdapter(List<RecordGroup> groups) {
        this.groups = groups;
    }

    @NonNull
    @Override
    public GroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_record_group, parent, false);
        return new GroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GroupViewHolder holder, int position) {
        RecordGroup group = groups.get(position);
        holder.tvDate.setText(daySdf.format(group.getDate()));
        holder.tvDayIncome.setText(String.format("收入: ¥%.2f", group.getIncomeTotal()));
        holder.tvDayExpense.setText(String.format("支出: ¥%.2f", group.getExpenseTotal()));
        holder.tvDayBalance.setText(String.format("结余: ¥%.2f", group.getDayBalance()));

        // 设置子项RecyclerView
        RecordAdapter childAdapter = new RecordAdapter(group.getRecords());
        holder.rvRecords.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
        holder.rvRecords.setAdapter(childAdapter);
    }

    @Override
    public int getItemCount() {
        return groups.size();
    }

    public static class GroupViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate, tvDayIncome, tvDayExpense, tvDayBalance;
        RecyclerView rvRecords;

        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvDayIncome = itemView.findViewById(R.id.tv_day_income);
            tvDayExpense = itemView.findViewById(R.id.tv_day_expense);
            tvDayBalance = itemView.findViewById(R.id.tv_day_balance);
            rvRecords = itemView.findViewById(R.id.rv_day_records);
        }
    }

    // 子项适配器
    private static class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.RecordViewHolder> {
        private List<Record> records;

        public RecordAdapter(List<Record> records) {
            this.records = records;
        }

        @NonNull
        @Override
        public RecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_record, parent, false);
            return new RecordViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecordViewHolder holder, int position) {
            Record record = records.get(position);
            holder.tvTime.setText(new SimpleDateFormat("HH:mm", Locale.CHINA).format(record.getDate()));
            holder.tvCategory.setText(record.getCategory());
            holder.tvAmount.setText(String.format("¥%.2f", record.getAmount()));
            holder.tvType.setText(record.getType() == Record.Type.INCOME ? "收入" : "支出");
            holder.tvType.setTextColor(holder.itemView.getContext().getResources().getColor(
                    record.getType() == Record.Type.INCOME ? android.R.color.holo_green_dark : android.R.color.holo_red_dark));
            holder.remark.setText(record.getNote());
        }

        @Override
        public int getItemCount() {
            return records.size();
        }

        public static class RecordViewHolder extends RecyclerView.ViewHolder {
            TextView tvTime, tvCategory, tvAmount, tvType, remark;

            public RecordViewHolder(@NonNull View itemView) {
                super(itemView);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvCategory = itemView.findViewById(R.id.tv_category);
                tvAmount = itemView.findViewById(R.id.tv_amount);
                tvType = itemView.findViewById(R.id.tv_type);
                remark = itemView.findViewById(R.id.remark);
            }
        }
    }
}